//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.ObjectBuilder;
using System.Windows.Shapes;
using LitwareHR.Infrastructure.Interface;
using LitwareHR.PublicApp.WinUX.Services;

using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
using System.Windows.Input;

using System.Threading;
using System.Windows.Media;
using System.Diagnostics;
using System.Collections.Generic;

using Shp.Runtime.Contract;

using Ricciolo.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using LitwareHR.PublicApp.WinUX.Entities;
using LitwareHR.PublicApp.WinUX.Search;
using Microsoft.Practices.CompositeUI.Utility;
using System.Globalization;

namespace LitwareHR.PublicApp.WinUX
{
    /// <summary>
    /// Interaction logic for JobSearchView.xaml
    /// </summary>
    public partial class JobSearchView : System.Windows.Controls.UserControl, IJobSearchView, IDisposable
    {
        private delegate void BindDataDelegate();

        /// <summary>
        /// Initialize a new instance of <see cref="JobSearchView"/>.
        /// <summary>
        public JobSearchView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoad);
            tab.Loaded += new RoutedEventHandler(tab_Loaded);
        }

        /// <summary>
        /// Called when WPF has loaded the control and is ready to display it.
        /// <summary>
        /// <param name="sender">Source of the event</param>
        /// <param name="e">Extra information about the event</param>
        void OnLoad(object sender, RoutedEventArgs e)
        {
            _presenter.OnViewReady();
        }

        /// <summary>
        /// Hide small one (CubeReflection) and display Large Cube (tab)
        /// </summary>
        private void ShowJobSearchCube()
        {
            Canvas.SetZIndex(tbDescription1, 1);    // Description of LitwareHR
            Canvas.SetZIndex(tbDescription2, 1);    // Description of LitwareHR
            Canvas.SetZIndex(CubeCanvasDetails, 1); // Large Cube with Previous and Next Buttons
            Canvas.SetZIndex(CanvasCover, 0);       // Cover (used as mask)
            Canvas.SetZIndex(CanvasGrids, -1);      // Canvas Containing both the grids (Open Positions & Desktop Search)
            Canvas.SetZIndex(spSelectedImageNSmallCube, -1);    // Selected Job Category Name, Image & Small Cube
        }

        /// <summary>
        /// Hide Large Cube (tab) and display small one (CubeReflection)
        /// </summary>
        private void HideJobSearchCube()
        {
            Canvas.SetZIndex(spSelectedImageNSmallCube, 1);
            Canvas.SetZIndex(CanvasGrids, 1);
            Canvas.SetZIndex(CanvasCover, 0);
            Canvas.SetZIndex(CubeCanvasDetails, -1);
            Canvas.SetZIndex(tbDescription1, -1);
            Canvas.SetZIndex(tbDescription2, -1);
        }

        /// <summary>
        /// This method is called on click of Mouse on Cube
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Cube_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Canvas cnv = sender as Canvas;
            if (cnv != null)
            {
                string strTitle = cnv.Tag.ToString();
                LargeCubeButtonClick();
                // Populate Grid with records of selected Title
                _presenter.FilterOpenPositions("TITLE", strTitle);
                if (checkBoxDesktopSearch.IsChecked == true)
                    _presenter.DesktopSearch(strTitle);
                _presenter.OnSelectedJob(strTitle);
            }
        }

        /// <summary>
        /// This method starts storyboard "Expand" and calls another function to Hide Job Search Cube
        /// </summary>
        private void LargeCubeButtonClick()
        {
            VisualBrush vb = new VisualBrush(tab);
            CubeReflection.Fill = vb;
            ((Storyboard)this.Resources["Expand"]).Begin(this);
            // Hide Large Cube (tab) and display small one (CubeReflection)
            HideJobSearchCube();
        }

        /// <summary>
        /// This mehtod is called on click of Cube Reflection (small Cube)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CubeReflection_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = sender as Rectangle;
            if (rect != null)
            {
                SmallButton_Clicked();
            }
        }

        /// <summary>
        /// This method is called when user clicks on Small Cube (Cube Reflection)
        /// </summary>
        private void SmallButton_Clicked()
        {
            ((Storyboard)this.Resources["Shrink"]).Begin(this);
            // Hide small one (CubeReflection) and display Large Cube (tab)
            ShowJobSearchCube();
            _presenter.DetachOpenPositionsFilter();
        }

        /// <summary>
        /// This method is called on click of search button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSearch_Click(object sender, RoutedEventArgs e)
        {
            string searchPattern = txtJobSearch.Text.Trim();
            LargeCubeButtonClick();
            // Field Name = string.Empty because
            // Onclick of Search button the search string should be compared with all fields.
            _presenter.FilterOpenPositions(string.Empty, searchPattern);

            if (checkBoxDesktopSearch.IsChecked == true)
                _presenter.DesktopSearch(searchPattern);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tab_Loaded(object sender, RoutedEventArgs e)
        {
            if (tab.ItemsSource == null)
                return;
            int i = 0;
            foreach (object item in tab.ItemsSource)
            {
                Item3D uiItem = tab.ItemContainerGenerator.ContainerFromItem(item) as Item3D;
                if (uiItem != null)
                {
                    CubeFace list = tab.Items[i] as CubeFace;
                    uiItem.Tag = list.Tiles;
                    i++;
                }
            }
        }

        #region IJobSearchView Members

        /// <summary>
        /// Initialize and display cube
        /// </summary>
        public void InitializeCube()
        {
            // Hide small one (CubeReflection) and display Large Cube (tab)
            ShowJobSearchCube();
        }

        public void LoadResources()
        {
            base.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"Litware.HR;;;component/Views/Skins/BaseSkin.xaml", UriKind.Relative)));
        }

        public void ShowOpenPositions(CollectionViewSource openPositions)
        {
            PositionView.DataContext = openPositions;
        }
        
        public void ShowWdsResults(WindowsDesktopSearchResultsCollection wdsResults)
        {
            WdsView.DataContext = wdsResults;
        }

        public void ShowCube(CubeFaceCollection cubeCollection)
        {
            tab.ItemsSource = cubeCollection;
        }

        /// <summary>
        /// This method Binds data to the Job Search Cube
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BindData()
        {
            if (!Dispatcher.CheckAccess())
            {
              Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new BindDataDelegate(BindData));
            }
            else
            {
                _presenter.BindData();
                tab_Loaded(this, null);

            }
        }

        /// <summary>
        /// Code To Display Selected Job Category Icon and Name
        /// </summary>
        /// <param name="strCubeFaceBtnCommandParameter"></param>
        public void DisplaySelectedJob(string title, BitmapImage image)
        {
            Guard.ArgumentNotNull(title, "title");

            string titleUppercase = title.ToUpper(CultureInfo.InvariantCulture);
            imgSelectedCategory.Source = image;
            tbSelectedCategoryName.Text = titleUppercase;
            tbSelectedCategoryNameShadow.Text = titleUppercase;
        }

        /// <summary>
        /// This method is used to Notify Schema Changes
        /// </summary>
        public void NotifySchemaChanges()
        {
            tbSchemaChanged.Visibility = Visibility.Visible;
            imgSchemaChanged.Visibility = Visibility.Visible;
            Storyboard StoryboardSchemaChanged = (Storyboard)FindResource("StoryboardSchemaChanged");
            StoryboardSchemaChanged.Begin(rootCanvas, true);
        }

        /// <summary>
        /// This method is used to start Synchronization Animation
        /// </summary>
        public void StartSyncAnimation()
        {
            imgSync.Visibility = Visibility.Visible;
            tbSync.Visibility = Visibility.Visible;
            Storyboard storyboardRotation = (Storyboard)FindResource("StoryboardRotation");
            storyboardRotation.Begin(rootCanvas, true);
        }

        /// <summary>
        /// This method is used to end Synchronization Animation
        /// </summary>
        public void EndSyncAnimation()
        {
            Storyboard storyboardRotation = (Storyboard)FindResource("StoryboardRotation");
            storyboardRotation.Stop(rootCanvas);
            Storyboard StoryboardEndSyncAnimation = (Storyboard)FindResource("StoryboardEndSyncAnimation");
            StoryboardEndSyncAnimation.Begin(rootCanvas);
        }

        #endregion
    }
}

